package com.jumi.microservice.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.controller.api.FeedbackController;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.dto.request.JmFeedBackQueryRequest;
import com.jumi.microservice.entity.JmFeedback;
import com.jumi.microservice.enums.FeedbackStatus;
import com.jumi.microservice.enums.MessageSendType;
import com.jumi.microservice.mq.SendMessageHandler;
import com.jumi.microservice.service.IJmFeedbackService;
import com.jumi.microservice.service.PageService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 会员反馈表 前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-08-06
 */
@RestController
@RequestMapping("/jm-feedback")
@Api(value="反馈管理",tags={"反馈操作接口"})
public class JmFeedbackController {

    @Autowired
    IJmFeedbackService feedbackService;

    @Resource
    SendMessageHandler sendMessageHandler;

    static final Logger logger = LoggerFactory.getLogger(FeedbackController.class);



    @PostMapping("/getFeedbackDataInfo")
    @ApiOperation("查询会员反馈分页")
    public TableDataInfo<JmFeedback> getFeedbackDataInfo(JmFeedBackQueryRequest request,
                                                 Integer size, Integer current){
        Page page=new Page(current,size);
        QueryWrapper wrapper= WrapperUtil.getWrapper(null,request);
        wrapper.orderByDesc("id");
        page=feedbackService.getPage(page,wrapper);
        return PageService.getDataTableInfo(page);
    }

    @PostMapping("/reply")
    @ApiOperation("反馈回复")
    public ResponseResult reply(Integer id, String replyContent) {
        JmFeedback byId = feedbackService.getById(id);
        if(ObjectUtil.isNull(byId))return ResponseResult.error("未查询到相关记录！");
        String loginName = ServletUtils.getLoginName();
        Long loginId = ServletUtils.getLoginId();
        byId.setReplyTime(LocalDateTime.now());
        byId.setReply(replyContent);
        byId.setReplyId(loginId);
        byId.setReplyName(loginName);
        byId.setStatus(FeedbackStatus.已回复);
        //推送消息
        try {
            MessageTemplate template = sendMessageHandler.getMessageTemplate(MessageSendType.REPLY.getValue());
            sendMessageHandler.sendMessage(template.getSmtAppContent(), template.getSmtMessageContent(), byId.getUid(), MessageSendType.REPLY.getValue());
        } catch (Exception e) {
            logger.info("消息发送失败!");
        }
        return ResponseResult.success(feedbackService.updateById(byId));
    }

    @PostMapping("/deleteFeedback")
    @ApiOperation("删除反馈")
    public ResponseResult deleteFeedback(@RequestParam String[] ids) {
        boolean re = feedbackService.removeByIds(CollectionUtil.list(true,ids));
        if (re) {
            return ResponseResult.success();
        }
        return ResponseResult.error();
    }

}
